package edu.bluebeanforum.controller;

import edu.bluebeanforum.common.AppResult;
import edu.bluebeanforum.common.ResultCode;
import edu.bluebeanforum.exception.ApplicationException;
import edu.bluebeanforum.mapper.BoardMapper;
import edu.bluebeanforum.model.Board;
import edu.bluebeanforum.service.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "板块接口")
@RequestMapping("/board")
public class BoardController {
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;
    @Resource
    private IBoardService boardService;


    @ApiOperation("获取首页板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("首页板块数："+indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
//        判断是否为空
        if (boards==null){
            boards=new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @ApiOperation("获取板块信息")
    @GetMapping("getById")
    public AppResult<Board> getById(@ApiParam("板块id") @RequestParam("id")  @NonNull Long id){
        Board board = boardService.selectById(id);
//        对查询结果做校验
        if(board==null||board.getDeleteState()==1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));

        }
        return AppResult.success(board);
    }

}
